$(document).ready(function() {
    /**
     * 处理图片显示方式的切换 - 列表和平铺
     */
    $('.gallery-show-type').on('click', function(){
        var $parent = $(this).parent().parent();
        if ($(this).hasClass('fa-th-large')) {
            /* 平铺显示 */
            $parent.find('.listing-gallery').hide();
            $parent.find('.tiled-gallery').show();
            $(this).removeClass('text-muted').addClass('text-primary');
            $(this).parent().find('.fa-th-list').removeClass('text-primary').addClass('text-muted');
        } else {
            /* 列表显示 */
            $parent.find('.tiled-gallery').hide();
            $parent.find('.listing-gallery').show();
            $(this).removeClass('text-muted').addClass('text-primary');
            $(this).parent().find('.fa-th-large').removeClass('text-primary').addClass('text-muted');
        }
    });
    
    /**
     * 双击打开文件夹
     */
    $('#box_card').on('dblclick', '.gallery-folder', function(){
        var card_id = $(this).data('card_id');
        var fid = $(this).data('fid');
        var t = $(this).data('type');
        var obj_nav = $('#nav_' + t + '_' + card_id);
        var obj_content = $('#edit_card_' + card_id).find('.gallery-content-' + t);
        var obj_item = $(this).parent().parent();
        var title_old = obj_item.find('.title').text();
        
        $.ajax({
            type:'post',
            url:'/attachmentadmin/folder/getChildren',
            data:'pid=' + fid + '&card_id=' + card_id,
            dataType:'json',
            timeout:60000,
            success:function(data){
                if (data.status == 0) {
                    /* 清空原有内容 */
                    obj_content.empty();
                    
                    /* 填充新内容 */
                    fillTiled(data.data, t, 'after', card_id);
                    fillListing(data.data, t, 'after', card_id);
                    
                    /* 处理导航 */
                    if (obj_nav.find('a').size() == 1) {
                        var str_nav = '<a href="javascript:;" data-card_id="' + card_id + '" data-fid="' + 
                                fid + '" data-type="' + t + '">' + title_old + '</a>';
                    } else {
                        var str_nav = '<a href="javascript:;" data-card_id="' + card_id + '" data-fid="' + 
                                fid + '" data-type="' + t + '">&nbsp;/&nbsp;' + title_old + '</a>';
                    }
                    obj_nav.append(str_nav);
                } else {
                    alert(data.error);
                }
                return false;
            }
        });
    });
    
    /**
     * 点击导航返回至某一文件夹
     */
    $('#box_card').on('click', '.gallery-nav > a', function(){
        var obj_a = $(this);
        var card_id = obj_a.data('card_id');
        var fid = obj_a.data('fid');
        var t = obj_a.data('type');
        var obj_content = $('#edit_card_' + card_id).find('.gallery-content-' + t);
        
        $.ajax({
            type:'post',
            url:'/attachmentadmin/folder/getChildren',
            data:'pid=' + fid + '&card_id=' + card_id,
            dataType:'json',
            timeout:60000,
            success:function(data){
                if (data.status == 0) {
                    /* 清空原有内容 */
                    obj_content.empty();
                    
                    /* 填充新内容 */
                    fillTiled(data.data, t, 'after', card_id);
                    fillListing(data.data, t, 'after', card_id);
                    
                    /* 处理导航 */
                    obj_a.nextAll('a').remove();
                } else {
                    alert(data.error);
                }
                return false;
            }
        });
    });
});


/**
 * 填充新的文件(夹)信息 - 平铺模式
 * 
 * @param obj d 数据
 * @param string t 文件类型：image、audio、vod
 * @param string s 文件夹插入位置。before：往前插入；after：往后插入；
 * @param string card_id 行动 ID
 */
function fillTiled(d, t, s, card_id) {
    /* 处理编辑 url 地址中的类型 */
    var t_url = t == 'video' ? 'vod' : t;
    
    /* 定义目录及文件模板 */
    var t_folder = '<div class="item" id="tiled_item_{#folder_id#}">' + 
            '<div class="text-center tiled-item-top">' + 
            '<i class="fa fa-folder-open cursor-pointer m-t gallery-folder" data-card_id="' + card_id + '" data-fid="{#fid#}" data-type="' + t + '"></i>' + 
            '</div>' + 
            '<div class="tiled-item-bottom">' + 
            '<div class="text-center padder-t-xs padder-b-xs tiled-info">' + 
            '<font class="title">{#title#}</font>' + 
            '<input type="text" class="form-control title-input default-hidden" value="{#title#}" data-folder_id="{#folder_id#}" data-show_type="tiled" />' + 
            '</div>' + 
            '<div class="btn-group btn-group-justified">' + 
            '<a class="btn btn-sm btn-orange action-rename" data-show_type="tiled">重命名</a>' + 
            '<a class="btn btn-sm btn-red action-delete" data-folder_id="{#folder_id#}" data-fid="{#fid#}" data-show_type="tiled">删除</a>' + 
            '</div></div></div>';
    
    var t_content = '<div class="item" id="tiled_item_{#a_id#}">';
    if (t == 'image') {
        t_content += '<div class="tiled-item-top">' + 
                '<a href="/workbenchadmin/' + t_url + '?_id={#a_id#}&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' + 
                '<img id="tiled_item_img_thumb_{#a_id#}" src="/uploads{#filepath#}" width="142" height="90" />' + 
                '</a>' + 
                '</div>';
    } else if (t == 'audio') {
        t_content += '<div class="text-center tiled-item-top">' + 
                '<a href="/workbenchadmin/' + t_url + '?_id={#a_id#}&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' + 
                '<i class="fa fa-music m-t gallery-folder"></i>' + 
                '</a>' + 
                '</div>';
    } else if (t == 'video') {
        t_content += '<div class="text-center tiled-item-top">' + 
                '<a href="/workbenchadmin/' + t_url + '?_id={#a_id#}&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' + 
                '{#video_cover#}' + 
                '</a>' + 
                '</div>';
    }
    t_content += '<div class="tiled-item-bottom">' + 
            '<div class="text-center padder-t-xs padder-b-xs tiled-info">{#title#}.{#suffix#}</div>' + 
            '<div class="btn-group btn-group-justified">' + 
            '<a class="btn btn-sm btn-orange" href="/workbenchadmin/' + t_url + '?_id={#a_id#}&card_id=' + card_id + '" target="_blank">编辑</a>' + 
            '<a class="btn btn-sm btn-info action-move" data-card_id="' + card_id + '" data-a_id="{#a_id#}" data-folder_hid="{#folder_hid#}" data-type="' + t + '">移动</a>' + 
            '<a class="btn btn-sm btn-lightblue" href="' + DOWNLOAD_URL + '?_id={#a_id#}" target="_blank">下载</a>' + 
            '</div></div></div>';
    
    /* 处理目录 */
    if (d.folder_info) {
        $(d.folder_info).each(function(k, v){
            /* 处理模板 */
            var str_folder = t_folder.replace(/{#folder_id#}/g, v._id);
            str_folder = str_folder.replace(/{#fid#}/g, v.fid);
            str_folder = str_folder.replace(/{#title#}/g, v.title);
            
            if (s == 'before') {
                $('#tiled_' + t + '_content_' + v.card_id).prepend(str_folder);
            } else {
                $('#tiled_' + t + '_content_' + v.card_id).append(str_folder);
            }
        });
    }
    
    /* 处理文件 */
    if (d.content_info) {
        $(d.content_info).each(function(k, v){
            /* 处理模板 */
            var str_content = t_content.replace(/{#a_id#}/g, v._id);
            str_content = str_content.replace(/{#filepath#}/g, v.filepath);
            str_content = str_content.replace(/{#title#}/g, v.title);
            str_content = str_content.replace(/{#suffix#}/g, v.suffix);
            str_content = str_content.replace(/{#folder_hid#}/g, v.folder_hid);
            
            /* 处理视频封面 */
            if (v.mediatype == 'video') {
                var str_cover = '<i class="fa fa-film m-t gallery-folder"></i>';
                if (v.extra.cover) {
                    str_cover = '<img src="/uploads' + v.extra.cover + '" width="142" height="90" />';
                }
                str_content = str_content.replace(/{#video_cover#}/g, str_cover);
            }
            
            $('#tiled_' + t + '_content_' + card_id).append(str_content);
        });
    }
}

/**
 * 填充新的文件(夹)信息 - 列表模式
 * 
 * @param obj d 数据
 * @param string t 文件类型：image、audio、vod
 * @param string s 文件夹插入位置。before：往前插入；after：往后插入；
 * @param string card_id 行动 ID
 */
function fillListing(d, t, s, card_id) {
    /* 处理编辑 url 地址中的类型 */
    var t_url = t == 'video' ? 'vod' : t;
    
    /* 定义目录及文件模板 */
    var t_folder = '<tr id="listing_item_{#folder_id#}">' + 
            '<td class="v-middle">' + 
            '<i class="fa fa-folder-open cursor-pointer m-t gallery-folder" data-card_id="' + card_id + '" data-fid="{#fid#}" data-type="' + t + '"></i>' + 
            '</td>' + 
            '<td class="v-middle">' + 
            '<font class="title">{#title#}</font>' + 
            '<input type="text" class="form-control title-input default-hidden" value="{#title#}" data-folder_id="{#folder_id#}" data-show_type="listing" />' + 
            '</td>' + 
            '<td class="v-middle">-</td>';
    if (t == 'audio' || t == 'video') {
        t_folder += '<td class="v-middle">-</td>';
    }
    t_folder += '<td class="v-middle">{#ctime#}</td>' + 
            '<td class="v-middle">' + 
            '<a class="btn btn-xs btn-orange action-rename" data-show_type="listing">' + 
            '<i class="fa fa-pencil"></i><span> 重命名</span>' + 
            '</a>&nbsp;' + 
            '<a class="btn btn-xs btn-red action-delete" data-folder_id="{#folder_id#}" data-fid="{#fid#}" data-show_type="listing">' + 
            '<i class="fa fa-trash-o"></i><span> 删除</span>' + 
            '</a>' + 
            '</td>' + 
            '</tr>';
    
    var t_content = '<tr id="listing_item_{#a_id#}">';
    if (t == 'image') {
        t_content += '<td class="v-middle">' + 
                '<a href="/workbenchadmin/image?_id={#a_id#}&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' + 
                '<img src="/uploads{#filepath#}" class="thumb-lg" border="0" />' + 
                '</a>' + 
                '</td>';
    } else if (t == 'audio') {
        t_content += '<td class="v-middle">' + 
                '<a href="/workbenchadmin/audio?_id={#a_id#}&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' + 
                '<i class="fa fa-music text-xxxl"></i>' + 
                '</a>' + 
                '</td>';
    } else if (t == 'video') {
        t_content += '<td class="v-middle">' + 
                '<a href="/workbenchadmin/vod?_id={#a_id#}&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' + 
                '{#video_cover#}' + 
                '</a>' + 
                '</td>';
    }
    t_content += '<td class="v-middle">{#title#}.{#suffix#}</td>';
    if (t == 'audio' || t == 'video') {
        t_content += '<td class="v-middle">{#duration#} 秒</td>';
    }
    t_content += '<td class="v-middle">{#filesize#}</td>' + 
            '<td class="v-middle">{#ctime#}</td>' + 
            '<td class="v-middle">' + 
            '<a href="/workbenchadmin/' + t_url + '?_id={#a_id#}&card_id=' + card_id + '" class="btn btn-xs btn-orange edit-single" target="_blank">' + 
            '<i class="fa fa-pencil"></i><span> 编辑</span>' + 
            '</a>&nbsp;' + 
            '<a class="btn btn-xs btn-info action-move" data-card_id="' + card_id + '" data-a_id="{#a_id#}" data-folder_hid="{#folder_hid#}" data-type="' + t + '">' + 
            '<i class="fa fa-pencil"></i><span>移动' + 
            '</a>&nbsp;' + 
            '<a href="' + DOWNLOAD_URL + '?_id={#a_id#}" class="btn btn-xs btn-lightblue" target="_blank">' + 
            '<i class="fa fa-download"></i><span> 下载</span>' + 
            '</a>' + 
            '</td>' + 
            '</tr>';
    
    /* 处理目录 */
    if (d.folder_info) {
        $(d.folder_info).each(function(k, v){
            /* 处理模板 */
            var str_folder = t_folder.replace(/{#folder_id#}/g, v._id);
            str_folder = str_folder.replace(/{#fid#}/g, v.fid);
            str_folder = str_folder.replace(/{#title#}/g, v.title);
            str_folder = str_folder.replace(/{#ctime#}/g, v.ctime);
            
            if (s == 'before') {
                $('#listing_' + t + '_content_' + v.card_id).find('tbody').prepend(str_folder);
            } else {
                $('#listing_' + t + '_content_' + v.card_id).find('tbody').append(str_folder);
            }
        });
    }
    
    /* 处理文件 */
    if (d.content_info) {
        $(d.content_info).each(function(k, v){
            /* 处理模板 */
            var str_content = t_content.replace(/{#a_id#}/g, v._id);
            str_content = str_content.replace(/{#filepath#}/g, v.filepath);
            str_content = str_content.replace(/{#title#}/g, v.title);
            str_content = str_content.replace(/{#suffix#}/g, v.suffix);
            str_content = str_content.replace(/{#folder_hid#}/g, v.folder_hid);
            str_content = str_content.replace(/{#duration#}/g, v.extra.duration);
            str_content = str_content.replace(/{#filesize#}/g, v.filesize_format);
            str_content = str_content.replace(/{#ctime#}/g, v.ctime);
            
            /* 处理视频封面 */
            if (v.mediatype == 'video') {
                var str_cover = '<i class="fa fa-film text-xxxl"></i>';
                if (v.extra.cover) {
                    str_cover = '<img src="/uploads' + v.extra.cover + '" class="thumb-lg" border="0" />';
                }
                str_content = str_content.replace(/{#video_cover#}/g, str_cover);
            }
            
            $('#listing_' + t + '_content_' + card_id).find('tbody').append(str_content);
        });
    }
}
